package org.zhiyootech.service;

import org.zhiyootech.entity.UserInfo;
import org.zhiyootech.repository.UserInfoRepository;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;

@ApplicationScoped
public class UserInfoService {

    @Inject
    UserInfoRepository userInfoRepository;

    /**
     * 根据openId 查询客户信息
     *
     * @param openId 微信返回
     * @return UserInfo
     */
    public UserInfo findByOpenId(String openId) {
        return userInfoRepository.findByOpenId(openId);
    }

    public long count() {
        return userInfoRepository.count();
    }

    @Transactional
    public boolean save(UserInfo userInfo) {
        try {
            userInfoRepository.persistAndFlush(userInfo);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
